package com.atguigu.myjuc.demo;


import java.util.concurrent.locks.ReentrantLock;

//可重入锁又名递归锁
//指的是在同一线程中，外层方法获取锁的时候，再进入该线程的内层方法会自动获取锁
//Java中ReentrantLock和synchronized都是可重入锁，可重入锁的一个优点是`可一定程度避免死锁`。
public class ReentrantLockDemo {

    public synchronized void a(){
        this.b();
        System.out.println("aaa");
    }

    public synchronized void b(){
        System.out.println("bbb");
    }



    //ReentrantLock版本演示可重入锁
    private final ReentrantLock lock = new ReentrantLock();
    public void c(){
        lock.lock();
        try {
            System.out.println("马上要掉d了");
            d();
        }finally {
            lock.unlock();
        }
    }
    public void d(){
        lock.lock();
        try {
            System.out.println("dddddddddddddddddddddddddd");
        }finally {
            lock.unlock();
        }
    }



    public static void main(String[] args) {
        new ReentrantLockDemo().c();
    }
}
